package org.test.client.ui.skinui.btn;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.IOException;

import org.jb2011.ninepatch4j.NinePatch;

/**
 * NinePatch扩展
 * 实现了 滤镜， 可以将图片颜色变深
 * @author Guodeqi
 *
 */
public class NinePatchExt {

	private static GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
	private static GraphicsConfiguration deCfg = environment.getDefaultScreenDevice().getDefaultConfiguration();
	
	public static NinePatch getNinePatch(String picPath) throws IOException {
		return NinePatch.load(NinePatchExt.class.getResource(picPath), false);
	}

	public static void drawRover(NinePatch np, Graphics2D g2d, int x, int y,int w, int h) {
		BufferedImage tmpPic = deCfg.createCompatibleImage(w, h, np.getImage().getTransparency());
		Graphics2D tmpG = (Graphics2D) tmpPic.getGraphics();
		np.draw(tmpG, 0, 0, w, h);

		//scales 参数 RGB alpha，0-1之间 想要颜色变深，则需 RGB分别小于1 
		float[] scales = { 0.9f, 0.9f, 0.9f, 1f };
		float[] offsets = new float[4];
		RescaleOp rop = new RescaleOp(scales, offsets, null);
		g2d.drawImage(tmpPic, rop, x, y);
	}
	
	public static void drawPressed(NinePatch np,Graphics2D g2d,int x, int y,int w, int h){
		BufferedImage tmpPic = deCfg.createCompatibleImage(w, h, np.getImage().getTransparency());
		Graphics2D tmpG = (Graphics2D) tmpPic.getGraphics();
		np.draw(tmpG, 0, 0, w, h);

		float[] scales = { 0.8f, 0.8f, 0.8f, 1f };
		float[] offsets = new float[4];
		RescaleOp rop = new RescaleOp(scales, offsets, null);
		g2d.drawImage(tmpPic, rop, x, y);
	}
	
}
